package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.characteristic.ALiYunSmsCharacteristic;
import com.xmy.cultivate.entity.StaffReg;
import com.xmy.cultivate.service.IStaffRegService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/api/staffReg")
public class StaffRegController {

    @Autowired
    @Lazy
    IStaffRegService iStaffRegService;

    @Resource
    ALiYunSmsCharacteristic aliyunSmsCharacteristic;

    @Value("${aliyun.sms.signNameString}")
    String signNameString;
    @Value("${aliyun.sms.templateCodeString}")
    String templateCodeString;
    @Value("${aliyun.AccessKeyID}")
    String accessKeyID;
    @Value("${aliyun.AccessKeySecret}")
    String accessKeySecret;

    @PostMapping("saveStaffReg")
    public Result saveStaffReg(@RequestBody StaffReg staffReg) {
        if (staffReg.getPhone() == null) {
            return new Result(Code.ERROR,"","手机号不能为空");
        }
        QueryWrapper<StaffReg> staffRegQueryWrapper = new QueryWrapper<>();
        staffRegQueryWrapper.eq("phone",staffReg.getPhone());
        staffRegQueryWrapper.last("LIMIT 1");
        StaffReg staffRegServiceOne = iStaffRegService.getOne(staffRegQueryWrapper);
        if (staffRegServiceOne != null) {
            String smg;
            if (staffRegServiceOne.getStatus() == 1) {
                smg = "手机号已经注册";
            } else if (staffRegServiceOne.getStatus() == 2) {
                smg = "审核结果："+staffRegServiceOne.getRefuseMsg()+"，注册失败，请与管理员联系";
            } else {
                smg = "审核中";
            }
            return new Result(Code.OK,"",smg);
        }
        boolean rs = iStaffRegService.save(staffReg);
        if (rs) {
            goToSmsString("17389712521", staffReg.getTeacherName());//暂时只发送给这个手机号码
        }
        return new Result(rs ? Code.OK : Code.ERROR,"", rs ? "注册成功，等待审核……" : "注册失败");
    }

    @ApiOperation("发送字符串账号")
    private boolean goToSmsString(String phone, String code) {
        return aliyunSmsCharacteristic.goToSmsName(accessKeyID,accessKeySecret,this.signNameString,this.templateCodeString,phone,code);
    }

    @PostMapping("editStaffReg")
    public Result editStaffReg(@RequestBody StaffReg staffReg) {
        if (staffReg.getId() == null) {
            return new Result(Code.ERROR,"","id不能为空");
        }
        if (staffReg.getStatus() == null) {
            return new Result(Code.ERROR,"","状态不能为空");
        }
        StaffReg staffRegServiceOne = iStaffRegService.getById(staffReg.getId());
        staffRegServiceOne.setStatus(staffReg.getStatus());
        if (staffReg.getRefuseMsg() != null && staffReg.getStatus() == 2) {
            staffRegServiceOne.setRefuseMsg(staffReg.getRefuseMsg());
        }
        if (staffReg.getStatus() == 2 && staffReg.getRefuseMsg() == null) {
            return new Result(Code.ERROR,"","拒绝内容不能为空");
        }
        boolean rs = iStaffRegService.updateById(staffRegServiceOne);
        String status = "";
        if (staffReg.getStatus() == 1) {
            status = "审核通过";
        }
        if (staffReg.getStatus() == 2) {
            status = "拒绝";
        }
        return new Result(rs ? Code.OK : Code.ERROR,"", rs ? status+"成功" : status+"失败");
    }

    @GetMapping("getStaffRegList")
    @ApiOperation("注册账号列表")
    public Result getStaffRegList(@RequestParam Map<String, Object> reMap) {
        int pageNum = reMap.get("pageNum") != null && !reMap.get("pageNum").toString().isEmpty() ? Integer.parseInt(reMap.get("pageNum").toString()) : 1;
        int pageSize = reMap.get("pageSize") != null && !reMap.get("pageSize").toString().isEmpty() ? Integer.parseInt(reMap.get("pageSize").toString()) : 10;
        IPage<StaffReg> iPage = new Page<>(pageNum, pageSize);

        String status = reMap.get("status") != null && !reMap.get("status").toString().isEmpty() ? reMap.get("status").toString() : "" ;
        String teacherName = reMap.get("teacherName") != null && !reMap.get("teacherName").toString().isEmpty() ? reMap.get("teacherName").toString() : "" ;
        String createdAt = reMap.get("createdAt") != null && !reMap.get("createdAt").toString().isEmpty() ? reMap.get("createdAt").toString() : "" ;
        QueryWrapper<StaffReg> staffRegQueryWrapper = new QueryWrapper<>();
        staffRegQueryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
        staffRegQueryWrapper.like(StringUtils.isNotBlank(teacherName), "teacher_name", teacherName);
        staffRegQueryWrapper.between(StringUtils.isNotBlank(createdAt), "createdAt", createdAt + " 00:00:00", createdAt + " 23:59:59");
        staffRegQueryWrapper.orderByDesc("id");
        iStaffRegService.page(iPage, staffRegQueryWrapper);
        return new Result(Code.OK,new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()),"列出注册账号列表成功");
    }

}
